package com.qf.service.impl;

import cn.hutool.core.date.DateUtil;
import com.qf.dao.DepositDao;
import com.qf.dao.LixiDao;
import com.qf.dao.UserBankCardDao;
import com.qf.dao.impl.DepositDaoImpl;
import com.qf.dao.impl.LixiDaoImpl;
import com.qf.dao.impl.UserBankCardDaoImpl;
import com.qf.model.Banktable;
import com.qf.model.Lixi;
import com.qf.model.UserCard;
import com.qf.service.DepositService;

import java.math.BigDecimal;
import java.util.List;

public class DepositServiceImpl implements DepositService {
    UserBankCardDao userBankCardDao = new UserBankCardDaoImpl();
    LixiDao lixidao = new LixiDaoImpl();
    DepositDao depositDao = new DepositDaoImpl();

    @Override
    public boolean depositMoney(Banktable banktable) {
        //查询是否已经有这张卡存的定期
        Banktable banktable1 = depositDao.findByCid(banktable.getCid());
        if (banktable1 != null) {
            return false;
        }
        //查银行卡表 是否存在银行卡
        UserCard userCard = userBankCardDao.findUserBankCard(banktable.getCid());
        if (userCard != null) {
            //有银行卡 进行存款
            BigDecimal money = userCard.getMoney();  //查询这个卡的余额
            int i = money.compareTo(banktable.getAmountMoney());//比较转的钱跟余额
            if (i != -1){
                //对转账银行卡进行扣款
                userCard.setMoney(money.subtract(banktable.getAmountMoney()));
                //调用业务层 修改银行卡余额
                int row1 = userBankCardDao.updateMoney(userCard);
                //获取当前转账时间
                String today = DateUtil.today();//获取存款日期
                banktable.setAmountTime(today);
                //获取利率
                Lixi lixi = lixidao.findRate(banktable.getDate());
                banktable.setRate(lixi.getRate());  // 将利率保存到存款对象中
                int row2 = depositDao.periodical(banktable);
                if (row1 > 0 && row2 > 0) {
                    //修改成功
                    return true;
                 }
            }
        }
        return false;
    }

    //通过银行卡号查询存款信息
    @Override
    public Banktable findDepositByCid(Banktable banktable) {
        return userBankCardDao.findDepositByCid(banktable);
    }

    @Override
    public int drawmoney(String cardnum, String outmoney) {

        return 0;
    }


}
